package com.universalInterface.lims_interface.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.universalInterface.common.exception.BusinessException;
import com.universalInterface.lims_interface.entity.LabChannel;
import com.universalInterface.lims_interface.entity.LabInfo;
import com.universalInterface.lims_interface.mapper.LabChannelMapper;
import com.universalInterface.lims_interface.mapper.LabInfoMapper;
import com.universalInterface.lims_interface.service.ILabChannelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 实验室通道
 * </p>
 *
 * @author author
 * @since 2023-12-19
 */
@RequiredArgsConstructor
@Service
public class LabChannelServiceImpl implements ILabChannelService {

    private final LabChannelMapper labChannelMapper;
    private final LabInfoMapper labInfoMapper;

    @Override
    public LabChannel save(LabChannel entity) {
        QueryWrapper query = new QueryWrapper();
        // 查询实验室是否存在
        query.eq("lab_guid", entity.getLabGuid());
        LabInfo labInfo = labInfoMapper.selectOne(query);
        if (labInfo == null) {
            throw new BusinessException(500, "实验室数据不存在");
        }
        entity.setChGuid(IdUtil.getSnowflakeNextId());
        int i = labChannelMapper.insert(entity);
        return entity;
    }

    @Override
    public Integer updateById(LabChannel entity) {
        // 不允许更新chGuid值
        if (entity.getChGuid() != null) {
            throw new BusinessException(500, "不允许更新chGuid值");
        }
        // 查询实验室是否存在
        QueryWrapper q = new QueryWrapper();
        q.eq("lab_guid", entity.getLabGuid());
        LabInfo labInfo = labInfoMapper.selectOne(q);
        if (labInfo == null) {
            throw new BusinessException(500, "实验室数据不存在");
        }
        return labChannelMapper.updateById(entity);
    }

    @Override
    public Integer deleteById(Long id) {
        return labChannelMapper.deleteById(id);
    }

    @Override
    public Integer saveBatch(List<LabChannel> entityList) {
        // 查询实验室是否存在
        List<Long> c = entityList.stream().map(x -> x.getLabGuid()).distinct().collect(Collectors.toList());
        QueryWrapper q = new QueryWrapper();
        q.in("lab_guid", c);
        List list = labInfoMapper.selectList(q);
        if (c.size() != list.size()) {
            throw new BusinessException(500, "实验室labGuid存在错误");
        }
        entityList.stream().forEach(x -> x.setChGuid(IdUtil.getSnowflakeNextId()));
        Integer i = labChannelMapper.saveBatch(entityList);
        return i;
    }

    @Override
    public Integer delete(LabChannel entity) {
        QueryWrapper q = new QueryWrapper();
        if (entity.getChGuid() != null) {
            q.eq("ch_guid", entity.getChGuid());
        }
        if (entity.getLabGuid() != null) {
            q.eq("lab_guid", entity.getLabGuid());
        }
        if (q.isEmptyOfWhere()) {
            return 0;
        }
        return labChannelMapper.delete(q);
    }

}
